package com.example.day5_vp;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    private Banner banner;
    List<String> imgs = new ArrayList<>();
    private ViewPager vp;
    List<Fragment> list = new ArrayList<>();
    MyFragAdapter myFragAdapter;
    List<Fragment> list2 = new ArrayList<>();
    MyFrag2Adapter myFrag2Adapter;
    private ViewPager2 vp2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        imgs.add("https://www.wanandroid.com/blogimgs/42da12d8-de56-4439-b40c-eab66c227a4b.png");
        imgs.add("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png");
        imgs.add("https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png");
        banner.setImages(imgs);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        banner.setDelayTime(2000);//设置时长
        //图片点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(MainActivity2.this, "点击了第" + position + "张图片", Toast.LENGTH_SHORT).show();
            }
        });
        banner.start();

        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());
        myFragAdapter = new MyFragAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(myFragAdapter);

        list2.add(new ThreeFragment());
        list2.add(new TwoFragment());
        list2.add(new OneFragment());
        myFrag2Adapter = new MyFrag2Adapter(this, list2);
        vp2.setAdapter(myFrag2Adapter);
        vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity2.this, "当前是"+(position+1), Toast.LENGTH_SHORT).show();
                super.onPageSelected(position);
            }

        });
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        vp = (ViewPager) findViewById(R.id.vp);
        vp2 = (ViewPager2) findViewById(R.id.vp2);
    }
}